package com.tbit.main.controller;

import com.tbit.common.enums.OperaPswEnum;
import com.tbit.common.group.UpdateGroup;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Result;
import com.tbit.main.service.OperaPswService;
import com.tbit.manager.api.AgentDepositVipApi;
import com.tbit.manager.api.dto.AgentDepositVipDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * @Description: 用户区域免押
 */
@RestController
@RequestMapping("/agentDepositVip")
@Validated
public class AgentDepositVipController {

    @Autowired
    private AgentDepositVipApi agentDepositVipApi;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 修改免押到期时间
     *
     * @param agentDepositVipDTO 用户区域免押实体类
     */
    @GetMapping("/editAgentDepositVip")
    public Result<String> edit(@Validated(UpdateGroup.class) AgentDepositVipDTO agentDepositVipDTO) {
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.USER_OPERA_UPDATE_MONEY.equals(agentDepositVipDTO.getOperaPsw())) {
            operaPswService.checkOperaPsw(LoginUserContextHolder.getLoginUser(), OperaPswEnum.OPERA_PSW_COUPONS_CONFIG.getModule(),
                    agentDepositVipDTO.getOperaPsw());
        }

        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        Integer accountUserId = loginUser.getAccountUserId();
        agentDepositVipDTO.setLoginUserAccountUserId(accountUserId);
        agentDepositVipApi.editAgentDepositVip(agentDepositVipDTO);
        return Result.success();
    }

    /**
     * 根据用户id和区域id获取免押卡
     *
     * @param userId    用户id
     * @param accountId 区域id
     */
    @GetMapping("/getByUserIdAndAccountId")
    public Result<AgentDepositVipDTO> getByUserIdAndAccountId(@NotNull Integer userId, @NotNull Integer accountId) {
        return Result.success(agentDepositVipApi.getByUserIdAndAccountId(userId, accountId));
    }
}
